package com.zlm.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: 郑
 * 线程池七大参数
 * public ThreadPoolExecutor(int corePoolSize, 核心线程数
 *                               int maximumPoolSize,最大线程数
 *                               long keepAliveTime,超时没人调用释放
 *                               TimeUnit unit,超时单位
 *                               BlockingQueue<Runnable> workQueue，阻塞队列
 *                               ThreadFactory threadFactory,线程工厂，创建线程的，一般不用动
 *                               RejectedExecutionHandler handler 拒绝策略)
 *
 */
public class Demo1 {
    public static void main(String[] args) {
        /**
         * //单一线程
         * public static ExecutorService newSingleThreadExecutor() {
         *         return new FinalizableDelegatedExecutorService
         *             (new ThreadPoolExecutor(1, 1,
         *                                     0L, TimeUnit.MILLISECONDS,
         *                                     new LinkedBlockingQueue<Runnable>()));
         *     }
         */
        ExecutorService pool = Executors.newSingleThreadExecutor();
        /**
         * //创建固定数量的线程池
         * public static ExecutorService newFixedThreadPool(int nThreads) {
         *         return new ThreadPoolExecutor(nThreads, nThreads,
         *                                       0L, TimeUnit.MILLISECONDS,
         *                                       new LinkedBlockingQueue<Runnable>());
         *     }
         */
        //ExecutorService pool = Executors.newFixedThreadPool(2);
        /**
         * //缓存线程池，可伸缩，遇强则强，遇弱则弱
         * public static ExecutorService newCachedThreadPool() {
         *         return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
         *                                       60L, TimeUnit.SECONDS,
         *                                       new SynchronousQueue<Runnable>());
         *     }
         */
        //ExecutorService pool = Executors.newCachedThreadPool();
        try {
            for (int i = 0; i < 10; i++) {
                //使用线程池创建线程
                pool.execute(() ->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //程序结束，关闭线程池
            pool.shutdown();
        }

    }
}
